﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MT_ToolKit
{
    public class EventManager : MonoSingleton<EventManager>
    {
        public delegate void event_handler(string event_name, object uData);
        private Dictionary<string, event_handler> dic = new Dictionary<string, event_handler>();

        public void Init()
        {

        }

        //注册事件
        public void AddListener(string event_name, event_handler h)
        {
            if (this.dic.ContainsKey(event_name))
            {
                this.dic[event_name] += h;
            }
            else
            {
                this.dic.Add(event_name, h);
            }
        }

        //删除事件
        public void RemoveListener(string event_name, event_handler h)
        {
            if (!this.dic.ContainsKey(event_name))
            {
                return;
            }

            this.dic[event_name] -= h;

            if (this.dic[event_name] == null)
            {
                this.dic.Remove(event_name);
            }
        }

        //发出事件
        public void Emit(string event_name, object uData)
        {
            if (!this.dic.ContainsKey(event_name)) return;

            this.dic[event_name](event_name, uData);
        }
    }
}

